/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techopropio.bfh.controller;

import com.techopropio.bfh.model.Usuario;
import com.techopropio.bfh.service.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Williams
 */
@WebServlet(name = "loginController", urlPatterns = {"/welcome.htm"})
public class LoginController extends HttpServlet {

    private LoginService loginService;
    private UrlService urlService;
    
    @Override
    public void init() throws ServletException {
        UsuarioDAO usuarioDAO = new UsuarioDAOSim();
        loginService = new LoginServiceImpl(usuarioDAO);
        urlService = new UrlServiceImpl();
    }   
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/views/Login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(true);
        Usuario usuario =  loginService.consultarUsuario(req.getParameter("login").toString(), req.getParameter("password").toString());
        if(usuario!=null){
            session.setAttribute("usuario", usuario);
            resp.sendRedirect("/BonFamHab2"+urlService.consultarURL(usuario.getRol()));
        }else{
            resp.sendRedirect("/BonFamHab2/welcome.htm");
        }
    }
    

}
